/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.niuweb.isad.core;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author niuxg
 */
public class Config {
    private static final Config config = new Config();
    
    private String _AppName;
    private String _AppDisplay;
    private String _AppLang;
    private String _AppLog;
    private String _AppMode;
    private String _DatabaseDriver;
    private String _DatabaseQuery;
    private String _DatabaseUrl;
    private String _DatabaseUser;
    private String _DatabasePassword;
    
    public static String getAppName() {
        return config._AppName;
    }
    public static String getAppDisplay() {
        return config._AppDisplay;
    }
    public static String getAppLang() {
        return config._AppLang;
    }
    public static String getAppLog() {
        return config._AppLog;
    }
    public static String getAppMode() {
        return config._AppMode;
    }
    public static String getDatabaseDriver() {
        return config._DatabaseDriver;
    }
    public static String getDatabaseQuery() {
        return config._DatabaseQuery;
    }
    public static String getDatabaseUrl() {
        return config._DatabaseUrl;
    }
    public static String getDatabaseUser() {
        return config._DatabaseUser;
    }
    public static String getDatabasePassword() {
        return config._DatabasePassword;
    }
    
    public static void update(HttpServletRequest req){
        config._AppName         = req.getParameter("project.name");
        config._AppDisplay         = req.getParameter("project.display");
        config._AppLang        = req.getParameter("project.lang");
        config._AppLog        = req.getParameter("project.log");
        config._AppMode        = req.getParameter("project.mode");
        config._DatabaseDriver    = req.getParameter("project.dbdriver");
        config._DatabaseQuery    = req.getParameter("project.dbtype");
        config._DatabaseUrl        = req.getParameter("project.dburl");
        config._DatabaseUser    = req.getParameter("project.dbuser");
        config._DatabasePassword= req.getParameter("project.dbpasswd");
        
        Element root = new Element("isad-app");
        
        Element app = new Element("app");
        
        Element app_name = new Element("name");
        app_name.setText(config._AppName);
        app.addContent(app_name);
        
        Element app_display = new Element("display");
        app_display.setText(config._AppDisplay);
        app.addContent(app_display);
        
        Element app_lang = new Element("lang");
        app_lang.setText(config._AppLang);
        app.addContent(app_lang);
        
        Element app_log = new Element("log");
        app_log.setText(config._AppLog);
        app.addContent(app_log);
        
        Element app_mode = new Element("mode");
        app_mode.setText(config._AppMode);
        app.addContent(app_mode);
        
        root.addContent(app);
        
        Element database = new Element("database");
        
        Element db_driver = new Element("driver");
        db_driver.setText(config._DatabaseDriver);
        database.addContent(db_driver);
        
        Element db_query = new Element("query");
        db_query.setText(config._DatabaseQuery);
        database.addContent(db_query);
        
        Element db_url = new Element("url");
        db_url.setText(config._DatabaseUrl);
        database.addContent(db_url);
        
        Element db_user = new Element("user");
        db_user.setText(config._DatabaseUser);
        database.addContent(db_user);
        
        Element db_password = new Element("password");
        db_password.setText(config._DatabasePassword);
        database.addContent(db_password);
        
        root.addContent(database);
        
        Document document = new Document(root);  
        XMLOutputter out = new  XMLOutputter();  

        try {
            File file = new File(Config.class.getResource("/isad.xml").getPath().replace("%20", " "));
            out.output(document, new FileOutputStream(file));  
        } catch (FileNotFoundException e) {  
        } catch (IOException e) {  
        }  
    }
    
    private Config(){
        SAXBuilder builder=new SAXBuilder(false);
        try {
            Document document = builder.build(Config.class.getResourceAsStream("/isad.xml"));
            Element root=document.getRootElement();
            Element app = root.getChild("app");
            this._AppName             = app.getChildText("name");
            this._AppDisplay         = app.getChildText("display");
            this._AppLang             = app.getChildText("lang");
            this._AppLog             = app.getChildText("log");
            this._AppMode            = app.getChildText("mode");
            Element database = root.getChild("database");
            this._DatabaseDriver     = database.getChildText("driver");
            this._DatabaseQuery     = database.getChildText("query");
            this._DatabaseUrl         = database.getChildText("url");
            this._DatabaseUser         = database.getChildText("user");
            this._DatabasePassword     = database.getChildText("password");
        } catch (JDOMException e) {
        } catch (IOException e) {
        }
    }
}